

var users = [];

module.exports = (io) => {
  //
  io.on('connection', (socket) => {
    socket
      .emit('connected')
      .on('check_user', (username, fn) => {
        let has = users.find(user => user === username);
        if(!has) {
          users.push(username);
        }
        fn(!!has, users);
        socket.broadcast.emit('user_join', username, users);
      })
      .on('user_left', (username) => {
        users = users.filter(user => user !== username);
        socket.emit('left', username);
        socket.broadcast.emit('user_left', username);
      })
      .on('send msg', (data) => {
        io.sockets.emit('msg', data);
      });
  });
};
